#!/usr/bin/env node
let network_status = "校园网已连接，未登录!"
async function notification_macos(title, content) {
    cmd = `display notification "${content}" with title "${title}"`
    call(["osascript", "-e", cmd])
}
async function try_login(username, password){
    await fetch(
        `http://10.3.191.9/drcom/login?callback=dr1003&DDDDD=${username}%40cau&upass=${password}&0MKKey=123456&R1=0&R3=0&R6=0&para=00&v6ip=&v=1574`
    )
}
async function try_reconnect() {
    try {
        host = socket.getaddrinfo("www.cau.edu.cn", 80)[0][4][0]
        if (!host.startswith('202')) 
            return "你使用的不是校园网!无需使用本软件"
        res = requests.get("http://10.3.191.9/", timeout=1)
        if (!res.text.contains("注销页"))
            return "校园网已连接，未登录!"
        else
            return "校园网已连接，已登录! "
    } catch (error) {
        return "网络环境异常!请检查"
    }
    
}
while (true) {
    network_status = try_reconnect()
    if (network_status == "校园网已连接，未登录!"){
        notification_macos("WARNING", str(datetime.datetime.now()) + "\\n校园网已经断开，正在尝试重连")
        try_login("2019307070109", "0x1D62814A29D")
        network_status = try_reconnect()
        if (network_status == "校园网已连接，已登录! ")
            notification_macos("INFO", str(datetime.datetime.now()) + "\\n校园网已经重连成功!")
    }
    print(network_status)
    sleep(3)
}

